package com.aaa.controller;

import com.aaa.pojo.Salary;
import com.aaa.service.SalaryService;
import com.aaa.utils.RespBean;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.swing.plaf.SliderUI;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * @author bSu
 * @date 2020/6/1 - 15:47
 */
@RestController
@RequestMapping("/salary")//工资管理_工资账套设置
public class SalaryController {

    @Autowired
    private SalaryService salaryService;

    ObjectMapper mapper = new ObjectMapper();

    @RequestMapping("/getAll")
    public RespBean getAll(Integer pageNum, Integer pageSize) throws JsonProcessingException {
        RespBean r=RespBean.build();
        try {
            PageInfo<Salary> info = salaryService.selectAll(pageNum, pageSize);
            System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(info));
            r.setStatus(200);
            r.setMsg("查询成功");
            r.setObj(info);
            return r;
        }catch (Exception e){
            r.setStatus(100);
            r.setMsg("查询失败");
            return r;
        }

    }

    @RequestMapping("/update")
    public RespBean update(Salary salary){
        RespBean r=RespBean.build();
        if(salary.getId()<=0){//为空或者不符合
            r.setStatus(100);
            r.setMsg("修改失败，参数错误");
            return r;
        }
        int i = salaryService.updateByPrimaryKeySelective(salary);
        if(i<=0){//受影响0行，一般不会出现这种情况
            r.setStatus(100);
            r.setMsg("修改失败");
            return  r;
        }
        r.setStatus(200);
        r.setMsg("修改成功");
        return r;
    }


    @RequestMapping("/addSalary")
    public RespBean addSalary(@Valid Salary salary, BindingResult result){//id自增

        RespBean r=RespBean.build();
        //获取jsr303验证的错误信息
        List<ObjectError> allErrors = result.getAllErrors();
        for (ObjectError error:allErrors) {
            String defaultMessage = error.getDefaultMessage();
            r.setStatus(100);
            r.setMsg(defaultMessage);
            return r;
        }
        //jsr303验证通过，向下执行
        int i = salaryService.insertSelective(salary);
        if(i<=0){
            r.setStatus(100);
            r.setMsg("添加失败");
            return r;
        }
        r.setMsg("添加成功");
        r.setStatus(202);
        return r;
    }

    @RequestMapping("/delSalary")
    public RespBean delSalary(Integer id){
        RespBean r=RespBean.build();
        int i = salaryService.deleteByPrimaryKey(id);
        if(i<=0){
            r.setStatus(100);
            r.setMsg("删除失败");
            return r;
        }
        r.setStatus(200);
        r.setMsg("删除成功");
        return r;
    }

    @RequestMapping("/getSalaryByid")
    public RespBean getSalaryByid(Integer id){
        RespBean r=RespBean.build();
        Salary salary = salaryService.selectByPrimaryKey(id);
        if(salary==null){
            r.setStatus(100);
            r.setMsg("查询失败");
        }
        r.setStatus(200);
        r.setMsg("成功成功");
        RespBean respBean = r.setObj(salary);
        return r;
    }

}
